import dayjs from "dayjs";

// 格式化时间
export const formateTime = (time, pattern = 'YYYY-MM-DD HH:mm:ss') => {
  return dayjs(time).format(pattern)
}

// 当天
export const todayDate = formateTime(Date.now(), 'YYYY-MM-DD')

// 三月前
export const threeMonthAgo = formateTime(Date.now() - 120 * 24 * 60 * 60 * 1000, 'YYYY-MM-DD')

//获取当前月份
export const getCurrentMonth = (time = Date.now()) => {
  return {
    startDate: dayjs(time).startOf('month').format('YYYY-MM-DD'),
    endDate: dayjs(time).endOf('month').format('YYYY-MM-DD'),
  }
}

// 判断两个时间区间是否有重复
export const judgeTimeHasRepate = (curTimes, oldTimes) => {
  const oldStartTime = new Date(oldTimes[0] + ' 00:00:00').getTime()
  const oldEndTime = new Date(oldTimes[1] + ' 23:59:59').getTime()
  const curStartTime = new Date(curTimes[0] + ' 00:00:00').getTime()
  const curEndTime = new Date(curTimes[0] + ' 23:59:59').getTime()
  // 有重叠
  if (curStartTime >= oldStartTime && curStartTime <= oldEndTime) return true
  if (curEndTime >= oldStartTime && curEndTime <= oldEndTime) return true
  return false
}